<?
class WebApplication {
    private $routeManager;
    private $filters = array();

    public function WebApplication(){
        $this->routeManager = new RouteManager();

        $this->registerControllers();
        $this->startSession();
    }

    public function action(){
        $url = $_GET['route'];

        $action = $this->routeManager->execute($url);

        $controller = $action[0];
        $method = $action[1];
        $vars = $action[2];

        foreach ($this->filters as $filter) {
            $filter->doFilter($url, &$controller, &$method, &$vars);
        }

        $controller->$method($vars);
    }

    private function startSession(){
        if (!isset($_SESSION)){
            session_start();
        }        
    }

    private function registerControllers(){
        foreach(Controller::listControllers() as $controller){
            $this->registerController($controller);
        }
    }

    public function registerController($controller){
        $this->routeManager->registerRoute(new Route(Controller::defaultRoute($controller), $controller));
    }

    public function registerRoute($route){
        $this->routeManager->registerRoute($route);
    }

    public function registerFilter($filter){
        $this->filters[] = $filter;
    }
}
?>